package com.kuang.controller;

import com.kuang.pojo.Books;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class userController {
    @Autowired
    @Qualifier("bookservice")
    private BookService bookService;

    //登录验证
    @RequestMapping("/dengLu")
    public String dengLu(String name, String password, HttpSession session, Model model){
//        List<Books> books = bookService.quertAllBook();
//        int i1 = Integer.parseInt(password);
//        session.setAttribute("username",name);
//        for (int i=0;i<books.size();i++){
//            if ((books.get(i).getBookName().equals(name))&&((books.get(i).getBookCounts())==i1)){
//                if (books.get(i).getDetail().equals("普通账户")){
//
//
//
//
//                    session.setAttribute("user","普通账户");
//                    return "redirect:/sale/getSale";
//                }if (books.get(i).getDetail().equals("管理员")){
//
//
//
//
//
//                 session.setAttribute("user","管理员");
//
//                    return "redirect:/sale/getSale";
//
//                }
//
//
//            }
//
//        }
        session.setAttribute("username","user");
        Books books = new Books(name, Integer.parseInt(password));

        Books booksbynp = bookService.getBooksbynp(books);
        if (booksbynp!=null){
            if (booksbynp.getDetail().equals("管理员")){
                session.setAttribute("user","管理员");
                return "redirect:/sale/getSale";
            }else {
                session.setAttribute("user", "普通账户");
                return "redirect:/sale/getSale";
            }
            }

        session.setAttribute("msgdenglu","密码或者账号不对，请重新输入");
        return "redirect:index.jsp";
    }
@RequestMapping("/tuichu")
public String tuichu(HttpSession session){
        session.removeAttribute("msgdenglu");
        session.removeAttribute("username");
        session.removeAttribute("user");
        return "redirect:index.jsp";
}
}
